﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIA.Configurators;
using SIA.HwAccess;

namespace SIA.Allarms
{

    /// <summary>Interfaccia Collezione di Allarmi</summary>
    public interface IAllarmsCollections : IList<IAllarm>
    {
        /// <summary>Quando l'allarme diventa Attivo</summary>
        event AllarmEvent RiseONAllarm;
        /// <summary>Quando l'allarme si spegne</summary>
        event AllarmEvent RiseOFFAllarm;
        /// <summary>Quando l'allarme è riconosciuto</summary>
        event AllarmEvent RiseACKAllarm;
        /// <summary>Quando l'allarme si è spento e ho avuto l'ACK</summary>
        event AllarmEvent RiseIsArchiviableAllarm;
        /// <summary>ListChange</summary>
        event AllarmEvent RiseAddAllarm;
        void sendACK();
    }

    public abstract class AbstractAllarmsCollections : List<IAllarm>, IAllarmsCollections
    {
        public event AllarmEvent RiseONAllarm;
        public event AllarmEvent RiseOFFAllarm;
        public event AllarmEvent RiseACKAllarm;
        public event AllarmEvent RiseIsArchiviableAllarm;
        public event AllarmEvent RiseAddAllarm;

        public new void Add(IAllarm allarm)
        {
            allarm.RiseONAllarm += new AllarmEvent(allarm_RiseONAllarm);
            allarm.RiseOFFAllarm += new AllarmEvent(allarm_RiseOFFAllarm);
            allarm.RiseACKAllarm += new AllarmEvent(allarm_RiseACKAllarm); 
            allarm.RiseIsArchiviableAllarm += new AllarmEvent(allarm_RiseIsArchiviableAllarm); 
            base.Add(allarm);
            if (this.RiseAddAllarm != null) this.RiseAddAllarm(allarm);
        }

        void allarm_RiseIsArchiviableAllarm(IAllarm allarm)
        {
            if (this.RiseIsArchiviableAllarm != null) this.RiseIsArchiviableAllarm(allarm);
        }

        void allarm_RiseACKAllarm(IAllarm allarm)
        {
            if (this.RiseACKAllarm != null) this.RiseACKAllarm(allarm);
        }

        void allarm_RiseOFFAllarm(IAllarm allarm)
        {
            if (this.RiseOFFAllarm != null) this.RiseOFFAllarm(allarm);
        }

        void allarm_RiseONAllarm(IAllarm allarm)
        {
            if (this.RiseONAllarm != null) this.RiseONAllarm(allarm);
        }
        
        public void sendACK()
        {
            foreach (IAllarm a in this.ToArray())
                a.sendACK();
        }
    }

    public class GenericAllarmsCollecions : AbstractAllarmsCollections
    {
        
    }

    public class ActiveAllarmsCollecions : AbstractAllarmsCollections
    {
        public ActiveAllarmsCollecions()
        {
            this.RiseIsArchiviableAllarm += new AllarmEvent(ActiveAllarmsCollecions_RiseIsArchiviableAllarm);
        }

        void ActiveAllarmsCollecions_RiseIsArchiviableAllarm(IAllarm allarm)
        {
            this.Remove(allarm);
        }
    }
}
